﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;

namespace Code4WP7SDK.Controls
{
    public class BindableApplicationBarIconButton : FrameworkElement,IApplicationBarIconButton
    {

        #region Event
        public event EventHandler Click;
        #endregion

        #region Constructor
        public BindableApplicationBarIconButton()
        {
            Button = new ApplicationBarIconButton();
            Button.Text = "Text";
            Button.Click += new EventHandler(Button_Click);
        }

        void Button_Click(object sender, EventArgs e)
        {
            if (Command != null && CommandParameter != null)
            {
                Command.Execute(CommandParameter);
            }
            else if (Command != null)
            {
                Command.Execute(CommandParameterValue);
            }
            if (Click != null)
            {
                Click(this, e);
            }
        }
        #endregion

        #region DependencyProperties
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(BindableApplicationBarIconButton),null);

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(BindableApplicationBarIconButton), null);


        public object CommandParameterValue
        {
            get { return (object)GetValue(CommandParameterValueProperty); }
            set { SetValue(CommandParameterValueProperty, value); }
        }

        public static readonly DependencyProperty CommandParameterValueProperty =
            DependencyProperty.Register("CommandParameterValue", typeof(object), typeof(BindableApplicationBarIconButton),null);


        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }

        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.Register("IsEnabled", typeof(bool), typeof(BindableApplicationBarIconButton), new PropertyMetadata(IsEnabledChanged));

        public static void IsEnabledChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                ((BindableApplicationBarIconButton)obj).Button.IsEnabled = (bool)e.NewValue;
            }
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(BindableApplicationBarIconButton), new PropertyMetadata(OnTextChanged));

        public static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                ((BindableApplicationBarIconButton)obj).Button.Text = e.NewValue.ToString();
            }
        }

        public new Visibility Visibility
        {
            get { return (Visibility)GetValue(VisibilityProperty); }
            set { SetValue(VisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Visibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty VisibilityProperty =
            DependencyProperty.Register("Visibility", typeof(Visibility), typeof(BindableApplicationBarIconButton), new PropertyMetadata(OnVisibilityChanged));

        public static void OnVisibilityChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //获取到父控件即BindableApplicationBar,设置Visibility属性
            BindableApplicationBarIconButton button = (BindableApplicationBarIconButton)obj;
            BindableApplicationBar bar = button.Parent as BindableApplicationBar;
            bar.Invalidate();
        }
        
        #endregion

        #region Properties

        public ApplicationBarIconButton Button { get; set; }
       
        public Uri IconUri
        {
            get
            {
                return Button.IconUri;
            }
            set
            {
                Button.IconUri = value;
            }
        }
        #endregion
    }
}
